#include "user.h"



User::User()
{
    m_id = 0;
    m_nom = "";
}
User::User(QString nom)
{
    m_id = 0;
    m_nom = nom;
}
User::User(QSqlQuery query)
{

    m_id = query.value(0).toInt();
    m_nom = query.value(1).toString();
    m_adresse = Adresse(query.value(2).toString(), query.value(3).toString(), query.value(4).toString(),
                          query.value(5).toString());
    m_contact = Contact(query, 7);
}
void User::setNom(QString nom)
{
    m_nom = nom;
}
void User::setAdresse(Adresse adresse)
{
    m_adresse = adresse;
}
void User::setContact(Contact contact)
{
    m_contact = contact;
}
QString User::getNom()const
{
    return m_nom;
}
Adresse User::getAdresse()const
{
    return m_adresse;
}
Contact User::getContact()const
{
    return m_contact;
}
////////////////////////
//
//  Operateur
//
////////////////////////
bool User::operator==(User const& user)
{
    return (m_nom.toLower() == user.getNom().toLower() && m_contact == user.getContact());
}
////////////////////////
//
//  SQL
//
////////////////////////
const QString User::NomColonneListeContact = QString("NomListeContact");
const QString User::NomChampUser = QString("NomUser");

void User::BindValues(QSqlQuery &query, QStringList champs)
{
    query.bindValue(":" + NomChampUser, m_nom);
    query.bindValue(":" + champs.at(1), m_adresse.getNumeroRue());
    query.bindValue(":" + champs.at(2), m_adresse.getRue());
    query.bindValue(":" + champs.at(3), m_adresse.getCodePostal());
    query.bindValue(":" + champs.at(4), m_adresse.getVille());
    query.bindValue(":" + champs.at(5), m_adresse.getDepartement());

//    qDebug() << "ok";
    m_contact.BindValues(query, Contact::ListeChampSQLSansId());

}
QStringList User::ListeChampSQL()
{
    QStringList l;
    l << NomChampId << NomChampUser << Adresse::ListeChampSQL() << Contact::ListeChampSQLSansId();
    return l;
}
QStringList User::ListeTypeDataSQL()
{
    QStringList l;
    l << TypeVarId << "varchar(30)" << Adresse::ListeTypeDataSQL() << Contact::ListeTypeDataSQLSansId();
    return l;
}
QStringList User::SQLWhere()const
{
    QStringList contactWhere = m_contact.SQLWhere();
    // remove clientId info
    contactWhere.removeFirst();
    contactWhere.removeFirst();
    QStringList l;
    l << NomChampUser << m_nom << contactWhere;
    return l;
}
QString User::SQLSet()const
{
    return NomChampUser + "='" + m_nom + "', " + m_adresse.SQLSet() + ", " + m_contact.SQLSet();
}
QString User::SetQuery()const
{
    return NomChampUser + "=:" + NomChampUser + ", " + m_adresse.SetQuery() + ", " + m_contact.SetQuery();
}
QStringList User::SetBindValues()const
{
    QStringList contactBindValues = m_contact.SetBindValues();
    // remove clientId info
    contactBindValues.removeFirst();
    contactBindValues.removeFirst();
    contactBindValues.removeLast();
    contactBindValues.removeLast();
    QStringList l;
    l << NomChampUser.toLower() << m_nom
      << m_adresse.SetBindValues()
      << contactBindValues
      << "id" << QString::number(m_id); // client id ? bug ?
    return l;
}
